package CSDN;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 王圆豪
 * Date: 2025-05-07
 * Time: 23:53
 */
public class Test15 {
    public static void main(String[] args) throws InterruptedException {
        Object l1 = new Object();
        Object l2 = new Object();
        Object l3 = new Object();

        Thread t1 = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                synchronized (l1) {
                    try {
                        l1.wait();
                        System.out.print("A");
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                synchronized (l2){
                    l2.notify();
                }
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                synchronized (l2) {
                    try {
                        l2.wait();
                        System.out.print("B");
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                synchronized (l3){
                    l3.notify();
                }
            }
        });
        Thread t3 = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                synchronized (l3) {
                    try {
                        l3.wait();
                        System.out.println("C");
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                synchronized (l1){
                    l1.notify();
                }
            }
        });

        t1.start();
        t2.start();
        t3.start();

        Thread.sleep(100);
        synchronized (l1){
            l1.notify();
        }
    }
}
